package com.slm.krstartimes.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.Toast;

import com.slm.krstartimes.R;
import com.slm.krstartimes.service.NotificationService;

/**
 * Created by dllo on 15/8/7.
 */
public class SettingActivity extends Activity implements View.OnClickListener, GestureDetector.OnGestureListener, View.OnTouchListener {
    private Button btn;
    private GestureDetector mGestureDetector;
    private int verticalMinDistance = 30;
    private int middleDistance = 150;
    private int minVelocity = 0;
    private LinearLayout setting_layout;
    private Switch service_btn, themeBtn;
    public static SharedPreferences preferences;
    private SharedPreferences.Editor editor;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
        preferences = getSharedPreferences("push", Context.MODE_MULTI_PROCESS);
        editor = preferences.edit();
        initView();
        if (preferences.getBoolean("open", true)) {
            service_btn.setChecked(true);
        } else {
            service_btn.setChecked(false);
        }
        if (preferences.getBoolean("close", true)) {
            service_btn.setChecked(true);
        } else {
            service_btn.setChecked(false);
        }
        btn.setOnClickListener(this);
        mGestureDetector = new GestureDetector(this);
        setting_layout.setOnTouchListener(this);
        setting_layout.setLongClickable(true);

        service_btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            /**
             * 推送服务开关
             * @param buttonView
             * @param isChecked
             */
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    editor.putBoolean("open", true);
                    editor.commit();
                    Intent intent = new Intent(SettingActivity.this, NotificationService.class);// 开启推送服务
                    startService(intent);
                } else {
                    editor.putBoolean("open", false);
                    editor.commit();
                    Intent intent = new Intent(SettingActivity.this, NotificationService.class);// 结束推送服务
                    stopService(intent);
                }
            }
        });
        themeBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    editor.putBoolean("close", true);
                    editor.commit();
                    Intent intent = new Intent(SettingActivity.this, NotificationService.class);// 开启推送服务
                    startService(intent);
                } else {
                    editor.putBoolean("close", false);
                    editor.commit();
                    Intent intent = new Intent(SettingActivity.this, NotificationService.class);// 结束推送服务
                    stopService(intent);
                }
            }
        });
    }

    /**
     * 初始化
     */
    private void initView() {
        btn = (Button) findViewById(R.id.btn);
        setting_layout = (LinearLayout) findViewById(R.id.setting_layout);
        service_btn = (Switch) findViewById(R.id.service_btn);
        themeBtn= (Switch) findViewById(R.id.themeBtn);
    }

    /**
     * 退出应用
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        finish();
        System.exit(0);
    }

    @Override
    public boolean onDown(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent motionEvent) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent motionEvent) {

    }

    /**
     * 滑动返回
     *
     * @param e1
     * @param e2
     * @param v
     * @param v1
     * @return
     */
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float v, float v1) {
        if (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(v) > minVelocity && (Math.abs(e2.getY() - e1.getY()) < middleDistance)) {
            finish();
        } else if (e1.getX() - e2.getX() > verticalMinDistance && Math.abs(v) > minVelocity && (Math.abs(e2.getY() - e1.getY()) < middleDistance)) {
            Toast.makeText(SettingActivity.this, "左滑", Toast.LENGTH_SHORT).show();
        }
        return false;
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return mGestureDetector.onTouchEvent(motionEvent);
    }
}
